package com.rongxun.hbwallet.mvp.presenter;


import android.support.annotation.NonNull;
import android.widget.Toast;

import com.rongxun.base.BasePresenter;
import com.rongxun.base.ResponseSubscriber;
import com.rongxun.hbwallet.mvp.contract.ILoadWallet;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.utils.RxUtil;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.ETHWalletUtils;
import com.rongxun.utils.wallet.WalletDaoUtils;
import com.rongxun.utils.wallet.WalletSubscriber;

import java.util.Arrays;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/8 14:52
 * 描述	      导入钱包
 */
public class LoadWalletPresenter extends BasePresenter<ILoadWallet.IView, ApiModel> implements ILoadWallet.IPresenter {


    public LoadWalletPresenter(@NonNull ILoadWallet.IView pUi) {
        super(pUi);
    }

    public LoadWalletPresenter(@NonNull ILoadWallet.IView pUi, @NonNull ApiModel pModel) {
        super(pUi, pModel);
    }

    @Override
    public void loadWalletByMnemonic(final String bipPath, final String mnemonic, final String pwd) {

        addSubscribe(Flowable.create((FlowableOnSubscribe<ETHWallet>) e -> {
            ETHWallet ethWallet = ETHWalletUtils.importMnemonic(bipPath
                    , Arrays.asList(mnemonic.split(" ")), pwd);
            if (ethWallet != null) {
                ethWallet.setBackup(true);
                WalletDaoUtils.insertNewWallet(ethWallet);
            }
            e.onNext(ethWallet);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<ETHWallet>(getView()) {
                    @Override
                    public void onSuccess(ETHWallet ethWallet) {
                        getView().loadSuccess(ethWallet);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {
                        Toast.makeText(getView().getContext(), "导入失败", Toast.LENGTH_SHORT).show();
                    }
                }));
    }

    @Override
    public void loadWalletByKeystore(final String keystore, final String pwd) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<ETHWallet>) e -> {
            ETHWallet ethWallet = ETHWalletUtils.loadWalletByKeystore(keystore, pwd);
            if (ethWallet != null) {
                ethWallet.setBackup(true);
                WalletDaoUtils.insertNewWallet(ethWallet);
            }
            e.onNext(ethWallet);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<ETHWallet>(getView()) {
                    @Override
                    public void onSuccess(ETHWallet ethWallet) {
                        getView().loadSuccess(ethWallet);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {
                        Toast.makeText(getView().getContext(), "导入失败", Toast.LENGTH_SHORT).show();
                    }
                }));
    }

    @Override
    public void loadWalletByPrivateKey(final String privateKey, final String pwd) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<ETHWallet>) e -> {
            ETHWallet ethWallet = ETHWalletUtils.loadWalletByPrivateKey(privateKey, pwd);
            if (ethWallet != null) {
                ethWallet.setBackup(true);
                WalletDaoUtils.insertNewWallet(ethWallet);
            }
            e.onNext(ethWallet);
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<ETHWallet>(getView()) {
                    @Override
                    public void onSuccess(ETHWallet ethWallet) {
                        getView().loadSuccess(ethWallet);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {
                        Toast.makeText(getView().getContext(), "导入失败", Toast.LENGTH_SHORT).show();
                    }
                }));
    }

    @Override
    public void saveWallet(String userId, ETHWallet wallet) {
        addSubscribe(getModel().saveWallet(userId, wallet.getAddress())
                .subscribeWith(new ResponseSubscriber<String>(getView()) {
                    @Override
                    public void onSuccess(String s) {
                        getView().showSaveWalletResult(wallet);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }
}
